package com.wys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wys.dao.UserDao;
import com.wys.pojo.User;
import com.wys.result.R;
import com.wys.service.UserService;
import com.wys.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
    // 邮件发送人
    @Value("${spring.mail.username}")
    private String from;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     *  发送邮箱验证码
     * @param user
     * @param session
     * @return
     */
    @Override
    public void sendMsg(User user, HttpSession session) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        //接受邮箱地址
        String mail = user.getPhone();
        //获取验证码
        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        /*//将验证码存储在session中
        session.setAttribute(mail,code);*/
        //邮件主题
        String subject = "中餐厅外卖订餐验证码";
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(code);
        simpleMailMessage.setTo(mail);
        simpleMailMessage.setFrom(from);
        //发送邮件
        javaMailSender.send(simpleMailMessage);
        //控制台输入验证码版
        /*String mail = user.getPhone();
        String code = ValidateCodeUtils.generateValidateCode(4).toString();*/
        //将验证码存储在session中

        //session.setAttribute(mail,code);
        //将验证码存储在redis中，并设置过期时间为两分钟
        redisTemplate.opsForValue().set(mail,code,5, TimeUnit.MINUTES);
        log.info(code);
    }

    /**
     *  手机端用户登录
     * @param map 登陆的邮箱和密码集合
     * @return
     */
    @Override
    public R<User> login(Map map,HttpSession session) {
        //获取用户输入的验证码
        String code = map.get("code").toString();
        //用户输入的邮箱
        String mail = map.get("phone").toString();
        //session中存储的验证码
        //Object sessionCode = session.getAttribute(mail);
        //redis中存储的验证码
        Object sessionCode = redisTemplate.opsForValue().get(mail);
        //验证码比对
        if(sessionCode != null && sessionCode.equals(code)){
            //比对成功，看是否是新用户
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getPhone,mail);
            User user = this.getOne(wrapper);
            if(user == null){
                //是新用户，把用户信息插入数据库，为用户进行注册
                user = new User();
                user.setPhone(mail);
                user.setStatus(1);
                this.save(user);
            }
            session.setAttribute("user",user.getId());
            //用户登陆成功，清除redis中的缓存
            redisTemplate.delete(mail);
            return R.success(user);
        }
        return R.error("登陆失败！");
    }
}
